

import { getUserList, UserDelete, UserSearch } from '@/services';
import { IsysUser } from '@/types';
import { Effect, Reducer } from 'umi';

export interface SysUserModelState {
  SysUserList:IsysUser[],
  SysUserListLength:number
}

export interface SysUserModelType {
  namespace: 'SysUser';
  state: SysUserModelState;
  effects: {
    getUserList: Effect;
    getUserSearch:Effect;
    SysUserDelete:Effect
  };
  reducers: {
    save: Reducer<SysUserModelState>;
  };
}

const SysUserModel: SysUserModelType = {
  namespace: 'SysUser',

  state: {
    SysUserList:[],
    SysUserListLength:0
  },

  effects: {
    *getUserList({ payload }, { call, put }) {
      let result = yield call(getUserList,payload);
      console.log('result......',result);
      if(result){
        yield put({
          type:"save",
          payload:{
            SysUserList:result.records.map((item:any)=>{
              item.key = item.userId;
              return item;
            })
          }
        })
      }
    },
    // 搜索
    *getUserSearch({payload},{call,put}){
      let {username} = payload;
      let result = yield call(UserSearch,username);
      console.log("search  result...",result);
      
      if(result){
        yield put({
          type:"save",
          payload:{
            SysUserList:result.records.map((item:any)=>{
              item.key = item.userId;
              return item;
            })
          }
        })
      }
    },
    *SysUserDelete({payload},{call,put}){
      let result  = yield call(UserDelete,payload);
      let res = yield call(getUserList,1);
      if(res){
        yield put({
          type:"save",
          payload:{
            SysUserList:res.records.map((item:any)=>{
              item.key = item.userId;
              return item;
            })
          }
        })
      }
    }
  },
  
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    
  }
  
};

export default SysUserModel;